const localStrategy = require('passport-local').Strategy;
const User =  require('../models/user');
const config = require('./database');
const bcrypt = require('bcrypt');

module.exports = function(passport) {
    // local strategy 
    passport.use(new localStrategy(function(username, password, done){
        // Match UserName 
        let query = {username: username},
        User.find(query, function(err, user){
            if(err) throw err
            if(!user) {
                return done(null, false, {message: 'nouser found'})
            }
        })


        bcrypt.compare(password, user.password, function(err, isMatch){
            if(err) throw err
            if(isMatch) {
                return done(null, user)
            }else {
                return done(null, false, {message: 'nouser found'});
            }
        })

    }))
}